import io

import pandas as pd


def to_camel_case(s):
    """
    """
    if not s or not isinstance(s, str):
        return s
    parts = s.split('_')
    camel_case = ''.join(part.capitalize() for part in parts if part)
    return camel_case


def find_new_table(old_table: str, df: pd.DataFrame):
    matching_rows = df[df['表代码-修改前'] == old_table]
    if matching_rows.empty:
        raise ValueError("未找到值为" + old_table + "的行")
    return matching_rows.iloc[0]['表代码'].lower()


def filter_diff(old_table: str, df: pd.DataFrame):
    filtered_df = df[df['表代码-修改前'] == old_table]
    diff_row = list()
    for index, row in filtered_df.iterrows():
        j_value = row['字段代码-修改前']
        i_value = row['字段代码']
        if pd.notna(j_value) and j_value.lower() != i_value.lower():
            print(f"J列值: {j_value}, I列值: {i_value}")
            diff_row.append(f"{j_value.lower()},{i_value.lower()}")
    return diff_row


if __name__ == '__main__':
    excel_name = "逻辑模型清单（ 开发组，配合数据标准管控整改，2025-08-28 1710 ）(1)(2)(1).xlsx"
    sheet_name = "逻辑模型清单"
    tableStr = """
    
    plugin_app_dir
    lm_agent_plugin_statistics
    lm_agent_plugin_share_item
    lm_agent_plugin_share
    lm_agent_plugin_release_version
    lm_agent_plugin_machine_statistics
    lm_agent_plugin_machine_example
    lm_agent_plugin_favorite
    lm_agent_plugin_add
    agt_script_info
    agt_script_info_release
    
    gaixc_agent_platform_agent_team_permission_info
    agent_teams
    agent_team_members
    agent_team_invite_code
    agent_team_user_log
    gaixc_agent_platform_apps_template_module
    gaixc_agent_platform_apps_template
    
    gaixc_agent_platform_app_bi_rel
    gaixc_agent_platform_agent_access_record
    gaixc_agent_hint_show
    ext_secret_interface
    ext_secret
    ext_interface
    
    editing_review_terms_lable
    editing_review_terms_expand
    editing_review_terms_content
    editing_review_terms
    dc_system_config_list
    dc_system_config
    code_model
    chats
    chat_items
    assistant_session_chat_items
    assistant_chat_items
    
    apps_favorite
    app_version_rel
    app_version_publish
    app_version_info
    app_version_draft
    app_mcp_server_release
    app_mcp_server
    app_mcp_jar_info_release
    app_mcp_jar_info
   
    app_flow_log
    ai_model
    agt_feedback_info
    agt_decision_info
    apps_label_hint
    
    
    gaixc_agent_platform_apps_template_module
    gaixc_agent_platform_apps_template
    
    plugin_app_fix_guess_release
    plugin_app_fix_guess
    
     app_mcp_info_release
     app_mcp_info
     
     apps_release
    apps
    
    
    """

    original_list = tableStr.split("\n")
    # 去除空元素并trim
    tableArr = [item.strip() for item in original_list if item and item.strip()]

    excel1 = pd.read_excel(excel_name, sheet_name=sheet_name, header=1)
    with open('gen/dataset_info2.txt', 'w', encoding='utf-8') as textIo:

        for table in tableArr:
            # file.write(f"\"{to_camel_case(table)}\",\n")
            textIo.write(f"\n####\n{to_camel_case(table)}\n")
            textIo.write(f"--\n{table},{find_new_table(table, excel1)}\n")
            textIo.write(f"--\n")
            diffs: list = filter_diff(table, excel1)
            for diff in diffs:
                textIo.write(f"{diff}\n")
